package Action.Manager;

import Action.Sys.Setting;
import Bean.AdministratorEntity;
import Bean.Log.SystemLogEntity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;

import java.util.LinkedList;
import java.util.List;

public class AltManagerInfo extends ActionSupport {
    String name;
    String oldpassword="";
    String newpassword="";
    AdministratorEntity administratorEntity;
    LinkedList<String> altLog=new LinkedList<>();
    Session session;
    @Override
    public String execute() throws Exception {

        session = HibernateTools.getSession();

        administratorEntity = session.get(AdministratorEntity.class, (int) ActionContext.getContext().getSession().get("adminid"));
        if (administratorEntity == null) {
            //二次验证用户身份
            HibernateTools.commit(session);
            return LOGIN;
        }

        //日志显示
        showAltLog(administratorEntity);


        if (name==null){
            //第一次打开--显示用户信息
            name=administratorEntity.getName();
            HibernateTools.commit(session);
            return INPUT;
        }
        if (!oldpassword.equals(administratorEntity.getPassword()))
        {
            addActionError("原密码错误");
            HibernateTools.commit(session);
            return ERROR;
        }
        //验证用户名是否有效
        if (!name.equals(administratorEntity.getName())){
            //用户名发生修改
            if (session.createQuery("from AdministratorEntity where name="+name).getResultList().size()>0){
                addActionError("用户名重复度,请更换");
                return ERROR;
            }
            administratorEntity.setName(name);
        }
        if (newpassword!=null&&!newpassword.equals("")){
            //密码修改
            if (newpassword.equals(administratorEntity.getPassword())){
                addActionError("原密码不能与旧密码相同");
                HibernateTools.commit(session);
                return ERROR;
            }
            administratorEntity.setPassword(newpassword);
        }
        session.saveOrUpdate(administratorEntity);
        addActionMessage("资料更新成功");
        SystemLogEntity.addLog(administratorEntity.getId(), null, Setting.SystemLog_flag_AltManagerData);
        showAltLog(administratorEntity);
        HibernateTools.commit(session);
        return SUCCESS;
    }

    private void showAltLog(AdministratorEntity administratorEntity) {
        altLog.clear();
        session=HibernateTools.getSession();
        List<SystemLogEntity> resultList = session.createQuery("from SystemLogEntity where userId=" + administratorEntity.getId() + " and flag= '" + Setting.SystemLog_flag_AltManagerData
        +"' order by id desc ")
                .setMaxResults(30)
                .getResultList();
        for (SystemLogEntity systemLogEntity : resultList) {
            altLog.addLast(systemLogEntity.BuildLog());
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNewpassword() {
        return newpassword;
    }

    public void setNewpassword(String newpassword) {
        this.newpassword = newpassword;
    }

    public String getOldpassword() {
        return oldpassword;
    }

    public void setOldpassword(String oldpassword) {
        this.oldpassword = oldpassword;
    }

    public AdministratorEntity getAdministratorEntity() {
        return administratorEntity;
    }

    public void setAdministratorEntity(AdministratorEntity administratorEntity) {
        this.administratorEntity = administratorEntity;
    }

    public LinkedList<String> getAltLog() {
        return altLog;
    }

    public void setAltLog(LinkedList<String> altLog) {
        this.altLog = altLog;
    }
}
